package com.nan.controller;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.json.FastJson;
import com.jfinal.plugin.activerecord.Page;
import com.nan.common.RespJson;
import com.nan.controller.base.BaseController;
import com.nan.interceptor.AdminInterceptor;
import com.nan.interceptor.AuthInterceptor;
import com.nan.model.ResType;

public class ResTypeController extends BaseController {

	public void list()
	{
		Long cate_id = getParaToLong("parent", (long) 0);
		int page = getParaToInt("page", 1);
		int page_size = 20;
		Page<ResType> type_list = ResType.dao.paginate(page, page_size, "SELECT * ", " FROM mm_res_type WHERE parent=? AND status=0", cate_id);
		
		if (cate_id != 0)
		{
			String parent_name = ResType.dao.findById(cate_id).getStr("name");
			setAttr("parent_name", parent_name);
		}
		
		setAttr("type_list", type_list);
		System.out.println(FastJson.getJson().toJson(type_list));
		setAttr("parent", cate_id);
		show("/views/cate-list.jsp");
	}
	
	@Before({AuthInterceptor.class, AdminInterceptor.class})
	public void edit()
	{
		String id = getPara("cate-id");
		if (id != null)
		{
			ResType res_type = ResType.dao.findById(id);
			setAttr("res_type", res_type);
		}
		
		String sql = "SELECT `id`, `name` FROM `mm_res_type` WHERE `parent`=0 AND `status`=0";
		List <ResType> type_list = ResType.dao.find(sql);
		setAttr("type_list", type_list);
		
		show("/views/cate-edit.jsp");
	}
	
	public void save()
	{
		String parent = getPara("parent").trim();
		String name = getPara("name").trim();
		String id = getPara("id").trim();
		
		if (!parent.equals("0") && ResType.dao.findById(parent) == null)
		{
			showJson(new RespJson(10001, "父类型不存在"));
			return ;
		}
		
		ResType res_type;
		boolean res;
		if (!id.equals("") && id != null)
		{
			res_type = ResType.dao.findById(id);
			res_type.set("parent", parent);
			res_type.set("name", name);
			res = res_type.update();
		}
		else
		{
			String sql = "SELECT `id` FROM `mm_res_type` WHERE `parent`=? AND `name`=? LIMIT 1";
			if (ResType.dao.findFirst(sql, parent, name) != null)
			{
				showJson(new RespJson(420, "已存在同名分类"));
				return ;
			}
			
			res_type = new ResType();
			res_type.set("parent", parent);
			res_type.set("name", name);
			res = res_type.save();			
		}
		
		System.out.println(res);
		if (res)
		{
			showJson(new RespJson(200));
			return ;
		}
		showJson(new RespJson(500, "保存失败"));
		
	}
	
	public void delete()
	{
		String id = getPara("id").trim();
		
		if (ResType.dao.findById(id) == null)
		{
			showJson(new RespJson(404, "操作的资源不存在"));
			return ;
		}
		
		ResType res_type = ResType.dao.findById(id);
		res_type.set("status", 1);
		if (res_type.update())
		{
			showJson(new RespJson(200));
			return ;
		}
		showJson(new RespJson(500, "删除失败"));
	}
	
	public void getSubType() {
		
		String parent = getPara("parent");
		
		String sql = "SELECT * FROM mm_res_type WHERE parent=? AND status=0";
		List<ResType> type_list = ResType.dao.find(sql, parent);
		
		showJson(new RespJson(200, "", type_list));
		
	}
}
